<template>
    <div class="last_audit">
        <!--终审移单-->
        <div class="search">
            <el-form ref="form" :inline="true" :model="form" :rules="rules">
                <el-form-item prop="lastJudgerId" :label="$t('authority.chooseLastAudit')"><!--选择终审-->
                    <el-select v-model="form.lastJudgerId" @change="changeJudger">
                        <el-option v-for="item in selectList" :label="item.name" :value="item.auditorId" :key="item.auditorId"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item prop="roleType" :label="$t('authority.allotTo')"><!--分配给-->
                    <el-select v-model="form.newJudgerId">
                        <el-option value="" :label="$t('verify.plsChoose')"></el-option>
                        <el-option v-for="item in filterAuditor" :label="item.name" :value="item.auditorId" :key="item.auditorId"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="move">{{$t('authority.allot')}}</el-button>
                    <el-button type="primary" @click="updateActiveTab">{{$t('authority.back')}}</el-button>
                </el-form-item>
            </el-form><!--end form-->
        </div>
        <div class="data_list">
            <el-table :data="tableData" :max-height="tableMaxHeight" style="width: 100%;" @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55"></el-table-column>
                <el-table-column prop="userName" :label="$t('common.name')" min-width="150"></el-table-column><!--姓名-->
                <el-table-column prop="userPhone" :label="$t('common.mobileNumber')" min-width="150"></el-table-column><!--手机号-->
                <el-table-column prop="userType" :label="$t('common.userType')" min-width="120"><!--用户类型-->
                    <template slot-scope="scope">{{ scope.row.userType | switchUserType }}</template>
                </el-table-column>
                <el-table-column prop="orderId" min-width="200" :label="$t('common.innerOrderId')">
                    <template slot-scope="{ row }">
                        <el-button @click="geDetail(row)" type="text">{{ row.orderId }}</el-button>
                    </template>
                </el-table-column><!--内部订单号-->
                <el-table-column prop="status" min-width="200" :label="$t('common.orderStatus')"><!--订单状态-->
                    <template slot-scope="scope">{{ scope.row.status |  switchStatus}}</template>
                </el-table-column>
                <el-table-column prop="contractAmount" :label="$t('common.contractAmount')" min-width="150"><!--合同金额-->
                    <template slot-scope="scope">{{ scope.row.contractAmount | formatCurrency }}</template>
                </el-table-column>
                <el-table-column prop="loanPeriod" :label="$t('common.borrowingCycle')" min-width="120"></el-table-column><!--借款期限-->
                <el-table-column prop="borrowTime" :label="$t('common.applyDate')" min-width="180"><!--申请日期-->
                    <template slot-scope="scope">{{ scope.row.borrowTime | formatDate('yyyy-MM-dd hh:mm:ss') }}</template>
                </el-table-column>
                <el-table-column prop="overdueHours" :label="$t('common.notProcessTime')" min-width="160"><!--未处理时长-->
                    <template slot-scope="scope">{{ isNaN(scope.row.overdueHours) ? '--' : (scope.row.overdueHours + 'h') }}</template>
                </el-table-column>
                <el-table-column prop="firstJudgerName" :label="$t('audit.firstReviewer')" min-width="160"></el-table-column><!--初审员-->
                <el-table-column prop="remark" :label="$t('common.remark')" min-width="120"><!--备注-->
                    <template slot-scope="scope">
                        <div class="edit_remark">
                            <span v-if="!scope.row.remark || (scope.row.remark && scope.row.remark.length <= 7)">{{ scope.row.remark}}</span>
                            <el-popover v-if="scope.row.remark && scope.row.remark.length > 7" trigger="hover" placement="top">
                                <p>{{ scope.row.remark }}</p>
                                <div slot="reference">
                                    <span v-if="scope.row.remark.length > 7">{{ scope.row.remark.substr(0, 7)}}...</span>
                                    <span v-else>{{ scope.row.remark }}</span>
                                </div>
                            </el-popover>
                        </div>
                    </template>
                </el-table-column>
                <!--<el-table-column :label="$t('common.operation')" width="100">&lt;!&ndash;操作&ndash;&gt;
                    <template slot-scope="scope">
                        <el-button @click="geDetail(scope.row)" type="text">{{$t('common.check')}}</el-button>
                    </template>
                </el-table-column>-->
            </el-table>
        </div>
        <pagination @pageChange="pageChange" :page="page">分页组件</pagination>
    </div>
</template>

<script>
    import Pagination from '@/components/pagination';
    import mixin from '@/service/mixin';

    let that = null;

    export default {
        name: 'orderLastAudit',
        components: {
            Pagination
        },
        mixins: [mixin],
        props: {
            activeTab: {
                type: String
            },
            searchForm: {
                type: Object
            }
        },
        data() {
            return {
                form: {
                    lastJudgerId: '',
                    newJudgerId: '',
                    status: ''
                },
                rules: {},
                selectList: [],
                selectedList: []
            };
        },
        watch: {
            activeTab(val) {
                if (val === 'orderLastAudit') {
                    this.form.lastJudgerId = this.searchForm.auditorId;
                    this.form.newJudgerId = '';
                    this.page.currentPage = 1;
                    this.getTableData();
                    this.getSelectOption();
                }
            }
        },
        computed: {
            filterAuditor() {
                if (this.form.lastJudgerId === this.form.newJudgerId) {
                    this.form.newJudgerId = '';
                }
                return this.selectList.filter(item => item.auditorId !== this.form.lastJudgerId && item.isDelete === 0);
            }
        },
        methods: {
            getTableData(page = this.page, form = this.form) {
                const params = {
                    // firstJudgerId: '',
                    lastJudgerId: form.lastJudgerId,
                    status: form.status,
                    orderStatus: '1',
                    page: {
                        pageNo: page.currentPage,
                        pageSize: this.page.pageSize
                    }
                };
                this.$api.audit.getAuditorOrderList(params).then((res) => {
                    const {status, data, error} = res;
                    if ('1' === status && '00000000' === error) {
                        this.tableData = data.data || [];
                    }
                });
            },
            changeJudger(val) {
                this.page.currentPage = 1;
                this.getTableData();
            },
            getSelectOption() {
                const params = {
                    type: 2,
                    name: '',
                    leaderId: '',
                    page: {
                        pageNo: 1,
                        pageSize: 10000
                    }
                };
                this.$api.authorityManage.getAllAuditorInfo(params).then((res) => {
                    const {status, data, error} = res;
                    if ('1' === status && '00000000' === error && data.data) {
                        this.selectList = data.data;
                    }
                });
            },
            move() {
                if (this.selectedList.length === 0) {
                    this.$message({
                        showClose: true,
                        message: this.$t('authority.chooseOrder'),
                        type: 'warning',
                    });
                    return false;
                }
                const newJudgerId = this.form.newJudgerId;
                if (!newJudgerId) {
                    this.$message({
                        showClose: true,
                        message: this.$t('audit.plsChooseAuditor'),
                        type: 'warning',
                    });
                    return false;
                }
                const params = {
                    newDistribute: newJudgerId,
                    assignType: 2,
                    orderId: this.selectedList
                };
                this.$api.audit.distribute(params).then((res) => {
                    const {status, error} = res;
                    if ('1' === status && '00000000' === error) {
                        this.$message({
                            message: this.$t('common.successfulOperation'),
                            showClose: true,
                            type: 'success',
                            duration: 1000,
                        });
                        this.getTableData();
                    }
                });
                return true;
            },
            handleSelectionChange(val) {
                // console.log(val);
                this.selectedList = [];
                val.forEach((item) => {
                    this.selectedList.push(item.orderId);
                });
                // console.log(this.selectedList);
            },
            updateActiveTab() {
                this.$emit('updateActiveTab', 'lastAudit');
            },
            geDetail(row) {
                this.$openNewBlank(`#/conf/audit/order-manage/audit-detail/${row.userId}/${row.orderId}/true`);
            }
        },
        filters: {
            switchUserType(type) {
                switch (+type) {
                    case 0:
                        return that.$t('common.newUser');
                    case 1:
                        return that.$t('common.secNewUser');
                    case 2:
                        return that.$t('common.oldUser');
                    default:
                        return '';
                }
            },
            switchStatus(type) {
                switch (type) {
                    case 'BORROW_AUDIT':
                        return that.$t('audit.firstReview');
                    case 'AUDIT_SUCCESS':
                        return that.$t('audit.finalReview');
                    case 'AUDIT_CHECK':
                        return that.$t('audit.secondReview');
                    default:
                        return '';
                }
            },
        },
        created() {
            that = this;
            if (this.activeTab === 'orderLastAudit') {
                this.form.lastJudgerId = this.searchForm.auditorId;
                this.page.currentPage = 1;
                this.getTableData();
                this.getSelectOption();
            }
        }
    };
</script>

<style lang="scss" scoped>
    .search {
        background-color: #fff;
        padding: 22px 40px 0;
    }

    .data_list {
        margin: 16px 24px;
        padding: 16px;
        background-color: #fff;
    }
</style>
